package com.retailshop.shopcontrol.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopcontrol.common.HttpPost;
import com.retailshop.shopcontrol.common.MD5Util;
import com.retailshop.shopcontrol.entity.HttpRequestConfig;
import com.retailshop.shopcontrol.entity.PostJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Controller
public class UserController {

    /**
     * 用户模块模块
     * 创建日期:2018年4月6日
     * 功能描述:通过request的方式来获取到json数据
     *
     * @param jsonobject 这个是阿里的 fastjson对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/userjson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String GetUserJson(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        try {
            // 直接将json信息打印出来
            System.out.println(jsonParam.toJSONString());
            //将接收到的数据转化为对象
            PostJson postJson = JSON.parseObject(jsonParam.toJSONString(), new TypeReference<PostJson>() {});
            String url = postJson.getUrl();
            //请求参数
            String parm = postJson.getParm();
            if(url.isEmpty() || parm.isEmpty()){
                return "{\"code\":999999,\"msg\":\"参数错误\",\"errorDetail\":\"参数错误\",\"count\":0,\"data\":[]}";
            }
            else{
                return GetUserMod(request,url,parm);
            }
        } catch (Exception ex) {
            return "{\"code\":999999,\"msg\":\"访问错误\",\"errorDetail\":\""+ex.getMessage()+"\",\"count\":0,\"data\":[]}";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/user")
    public String GetUser(HttpServletRequest request) {
        try {
            //请求地址
            String url=request.getParameter("url");
            //请求参数
            String parm="";

            //读取所有参数
            Enumeration paramNames = request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();
                if(!paramName.equals("url")){
                    parm +=",\""+paramName+"\":\""+request.getParameter(paramName)+"\"";
                }
            }

            System.out.println("parm:"+parm);

            if(url.isEmpty() || url==""){
                return "{\"code\":999999,\"msg\":\"参数错误\",\"errorDetail\":\"参数错误\",\"count\":0,\"data\":[]}";
            }else{
                if(parm.length()>1){
                    parm = parm.substring(1);
                }
                parm ="{"+parm+"}";
                return GetUserMod(request,url,parm);
            }
        } catch (Exception ex) {
            return "{\"code\":999999,\"msg\":\"访问错误\",\"errorDetail\":\""+ex.getMessage()+"\",\"count\":0,\"data\":[]}";
        }
    }

    public String GetUserMod(HttpServletRequest request, String url, String parm) {
        //模块URL
        String modUrl = HttpRequestConfig.userServerUrl;
        //模块密钥
        String modKey = "63cca9daa815b390f62f35f92e4d5392";

        return HttpPost.GetMod(request,modUrl,modKey,url,parm);
    }

    @ResponseBody
    @RequestMapping(value = "/GetPwd")
    public String GetPwd(HttpServletRequest request) {
        String pwd = request.getParameter("pwd");
        try{
            pwd= MD5Util.getEncryptedPwd(pwd);
            System.out.println("密码："+pwd);
            return pwd;

        }catch (Exception e){
            return pwd;
        }
    }

}
